﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;

namespace MyCompany.MyProject.Common.Helper
{
    /// <summary>
    /// 泛型递归求树行结构
    /// </summary>
    public static class RecursionHelper
    {
        /// <summary>
        /// 菜单权限树
        /// </summary>
        /// <param name="all"></param>
        /// <param name="current"></param>
        /// <param name="pid"></param>
        /// <param name="needbtn"></param>
        public static void LoopToAppendChildren(List<PermissionTree> all,PermissionTree current,long pid,bool needbtn)
        {
            var subItems = all.Where(i => i.Pid == current.value).ToList();
            var btnItems = subItems.Where(i => i.isbtn == true).ToList();
            if (subItems.Count > 0)
            {
                current.btns = new List<PermissionTree>();
                current.btns.AddRange(btnItems);
            }
            else
            {
                current.btns = null;
            }
            if (!needbtn)
            {
                subItems = subItems.Where(i => i.isbtn == false).ToList();
            }
            if (subItems.Count > 0)
            {
                current.children = new List<PermissionTree>();
                current.children.AddRange(subItems);
            }
            else
            {
                current.children = null;
            }
            //if (current.isbtn)
            //{
            //    //curItem.label += "按钮";
            //}

            foreach (var item in subItems)
            {
                if(item.value == pid && pid > 0)
                {
                    //subItem.disabled = true;//禁用当前节点
                }
                LoopToAppendChildren(all, item, pid, needbtn);
            }
        }

        /// <summary>
        /// 循环给树添加子节点
        /// </summary>
        /// <param name="all"></param>
        /// <param name="curItem"></param>
        /// <param name="pid"></param>
        public static void LoopToAppendChildren(List<DepartmentTree>all,DepartmentTree curItem,long pid)
        {
            var subItems = all.Where(e=>e.Pid==curItem.value).ToList();
            if (subItems.Count() > 0)
            {
                curItem.children = new List<DepartmentTree>();
                curItem.children.AddRange(subItems);
            }
            else
            {
                curItem.children = null;
            }

            foreach (var subItem in subItems)
            {
                if (subItem.value == pid && pid > 0)
                {
                    // 禁用当前节点
                    subItem.disabled = true;
                }
                LoopToAppendChildren(all,subItem,pid);
            }
        }

        /// <summary>
        /// 前端导航栏树
        /// </summary>
        /// <param name="all"></param>
        /// <param name="curItem"></param>
        public static void LoopToAppendChildren(List<NavigationBar> all, NavigationBar curItem)
        {
            var subItems = all.Where(e => e.pid == curItem.id).OrderBy(e=>e.order).ToList();
            if (subItems.Count() > 0)
            {
                curItem.children = new List<NavigationBar>();
                curItem.children.AddRange(subItems);
            }
            else
            {
                curItem.children = null;
            }
            foreach (var subItem in subItems)
            {
                LoopToAppendChildren(all, subItem);
            }
        }
    }

    /// <summary>
    /// 部门数model
    /// </summary>
    public class DepartmentTree
    {
        public long value { get; set; }
        public long Pid { get; set; }
        public string label { get; set; }
        public int order { get; set; }
        public bool disabled { get; set; }
        public List<DepartmentTree> children { get; set; }
    }

    /// <summary>
    /// 菜单权限树model
    /// </summary>
    public class PermissionTree
    {
        public long value { get; set; }

        public long Pid { get; set; }

        public string? label { get; set; }

        public int order { get; set; }

        public bool disabled { get; set; }

        public bool  isbtn { get; set; }

        public List<PermissionTree>? children { get; set; }

        public List<PermissionTree>? btns { get; set; }
    }

    public class  NavigationBar
    {
        public long id { get; set; }
        public long pid { get; set; }
        public int order { get; set; }
        public string? name { get; set; }
        public bool IsHide { get; set; } = false;
        public bool IsButton { get; set; } = false;
        public string? path { get; set; }

        public string? Func { get; set; }
        public string? iconCls { get; set; }
        public NavigationBarMeta? meta { get; set; }
        public List<NavigationBar>? children { get; set; }
    }
    public class NavigationBarMeta
    {
        public string? title { get; set; }
        public bool requireAuth { get; set; }=false;
        public bool NoTabPage { get; set; } = false;
        public bool keepAlive { get; set; }= false;
        public string? icon { get; set; }
    }
}
